| Conditions | 34 |
| Total Lines | 360 |
| Code Lines | 232 |
| Lines | 172 |
| Ratio | 47.78 % |
| Changes | 0 | ||
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
Complex classes like script.js ➔ createMap often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
| 1 | /* |
||
| 71 | function createMap(mapOpts, poi) { |
||
| 72 | |||
| 73 | // const mapOpts = olMapData[0].mapOpts; |
||
| 74 | // const poi = olMapData[0].poi; |
||
| 75 | |||
| 76 | if (!olEnable) { |
||
|
|
|||
| 77 | return; |
||
| 78 | } |
||
| 79 | if (!olTestCSSsupport()) { |
||
| 80 | olEnable = false; |
||
| 81 | return; |
||
| 82 | } |
||
| 83 | |||
| 84 | // find map element location |
||
| 85 | var cleartag = document.getElementById(mapOpts.id + '-clearer'); |
||
| 86 | if (cleartag === null) { |
||
| 87 | return; |
||
| 88 | } |
||
| 89 | // create map element and add to document |
||
| 90 | var fragment = olCreateMaptag(mapOpts.id, mapOpts.width, mapOpts.height); |
||
| 91 | cleartag.parentNode.insertBefore(fragment, cleartag); |
||
| 92 | |||
| 93 | /** dynamic map extent. */ |
||
| 94 | let extent = ol.extent.createEmpty(); |
||
| 95 | overlayGroup = new ol.layer.Group({title: 'Overlays', fold: 'open', layers: []}); |
||
| 96 | const baseLyrGroup = new ol.layer.Group({'title': 'Base maps', layers: []}); |
||
| 97 | |||
| 98 | const map = new ol.Map({ |
||
| 99 | target: document.getElementById(mapOpts.id), |
||
| 100 | layers: [baseLyrGroup, overlayGroup], |
||
| 101 | view: new ol.View({ |
||
| 102 | center: ol.proj.transform([mapOpts.lon, mapOpts.lat], 'EPSG:4326', 'EPSG:3857'), |
||
| 103 | zoom: mapOpts.zoom, |
||
| 104 | projection: 'EPSG:3857' |
||
| 105 | }) |
||
| 106 | }); |
||
| 107 | |||
| 108 | View Code Duplication | if (osmEnable) { |
|
| 109 | baseLyrGroup.getLayers().push( |
||
| 110 | new ol.layer.Tile({ |
||
| 111 | visible: true, |
||
| 112 | title: 'OSM', |
||
| 113 | type: 'base', |
||
| 114 | source: new ol.source.OSM() |
||
| 115 | })); |
||
| 116 | |||
| 117 | baseLyrGroup.getLayers().push( |
||
| 118 | new ol.layer.Tile({ |
||
| 119 | visible: mapOpts.baselyr === "cycle map", |
||
| 120 | title: 'cycle map', |
||
| 121 | type: 'base', |
||
| 122 | source: new ol.source.OSM({ |
||
| 123 | url: 'https://{a-c}.tile.thunderforest.com/cycle/{z}/{x}/{y}.png?apikey=' + tfApiKey, |
||
| 124 | attributions: 'Data ©ODbL <a href="https://openstreetmap.org/copyright" target="_blank">OpenStreetMap</a>, ' |
||
| 125 | + 'Tiles ©<a href="https://www.thunderforest.com/" target="_blank">Thunderforest</a>' |
||
| 126 | + '<img src="https://www.thunderforest.com/favicon.ico" alt="Thunderforest logo"/>' |
||
| 127 | }) |
||
| 128 | })); |
||
| 129 | |||
| 130 | baseLyrGroup.getLayers().push( |
||
| 131 | new ol.layer.Tile({ |
||
| 132 | visible: mapOpts.baselyr === "transport", |
||
| 133 | title: 'transport', |
||
| 134 | type: 'base', |
||
| 135 | source: new ol.source.OSM({ |
||
| 136 | url: 'https://{a-c}.tile.thunderforest.com/transport/{z}/{x}/{y}.png?apikey=' + tfApiKey, |
||
| 137 | attributions: 'Data ©ODbL <a href="https://openstreetmap.org/copyright" target="_blank">OpenStreetMap</a>, ' |
||
| 138 | + 'Tiles ©<a href="https://www.thunderforest.com/" target="_blank">Thunderforest</a>' |
||
| 139 | + '<img src="https://www.thunderforest.com/favicon.ico" alt="Thunderforest logo"/>' |
||
| 140 | }) |
||
| 141 | })); |
||
| 142 | |||
| 143 | baseLyrGroup.getLayers().push( |
||
| 144 | new ol.layer.Tile({ |
||
| 145 | visible: mapOpts.baselyr === "landscape", |
||
| 146 | title: 'landscape', |
||
| 147 | type: 'base', |
||
| 148 | source: new ol.source.OSM({ |
||
| 149 | url: 'https://{a-c}.tile.thunderforest.com/landscape/{z}/{x}/{y}.png?apikey=' + tfApiKey, |
||
| 150 | attributions: 'Data ©ODbL <a href="https://openstreetmap.org/copyright" target="_blank">OpenStreetMap</a>, ' |
||
| 151 | + 'Tiles ©<a href="https://www.thunderforest.com/" target="_blank">Thunderforest</a>' |
||
| 152 | + '<img src="https://www.thunderforest.com/favicon.ico" alt="Thunderforest logo"/>' |
||
| 153 | }) |
||
| 154 | })); |
||
| 155 | |||
| 156 | baseLyrGroup.getLayers().push( |
||
| 157 | new ol.layer.Tile({ |
||
| 158 | visible: mapOpts.baselyr === "outdoors", |
||
| 159 | title: 'outdoors', |
||
| 160 | type: 'base', |
||
| 161 | source: new ol.source.OSM({ |
||
| 162 | url: 'https://{a-c}.tile.thunderforest.com/outdoors/{z}/{x}/{y}.png?apikey=' + tfApiKey, |
||
| 163 | attributions: 'Data ©ODbL <a href="https://openstreetmap.org/copyright" target="_blank">OpenStreetMap</a>, ' |
||
| 164 | + 'Tiles ©<a href="https://www.thunderforest.com/" target="_blank">Thunderforest</a>' |
||
| 165 | + '<img src="https://www.thunderforest.com/favicon.ico" alt="Thunderforest logo"/>' |
||
| 166 | }) |
||
| 167 | })); |
||
| 168 | } |
||
| 169 | |||
| 170 | View Code Duplication | if (bEnable && bApiKey !== '') { |
|
| 171 | baseLyrGroup.getLayers().push( |
||
| 172 | new ol.layer.Tile({ |
||
| 173 | visible: mapOpts.baselyr === "bing road", |
||
| 174 | title: 'bing road', |
||
| 175 | type: 'base', |
||
| 176 | source: new ol.source.BingMaps({ |
||
| 177 | key: bApiKey, |
||
| 178 | imagerySet: 'Road' |
||
| 179 | }) |
||
| 180 | })); |
||
| 181 | |||
| 182 | baseLyrGroup.getLayers().push( |
||
| 183 | new ol.layer.Tile({ |
||
| 184 | visible: mapOpts.baselyr === "bing sat", |
||
| 185 | title: 'bing sat', |
||
| 186 | type: 'base', |
||
| 187 | source: new ol.source.BingMaps({ |
||
| 188 | key: bApiKey, |
||
| 189 | imagerySet: 'Aerial' |
||
| 190 | }) |
||
| 191 | })); |
||
| 192 | |||
| 193 | baseLyrGroup.getLayers().push( |
||
| 194 | new ol.layer.Tile({ |
||
| 195 | visible: mapOpts.baselyr === "bing hybrid", |
||
| 196 | title: 'bing hybrid', |
||
| 197 | type: 'base', |
||
| 198 | source: new ol.source.BingMaps({ |
||
| 199 | key: bApiKey, |
||
| 200 | imagerySet: 'AerialWithLabels' |
||
| 201 | }) |
||
| 202 | })); |
||
| 203 | } |
||
| 204 | |||
| 205 | if (stamenEnable) { |
||
| 206 | baseLyrGroup.getLayers().push( |
||
| 207 | new ol.layer.Tile({ |
||
| 208 | visible: false, |
||
| 209 | type: 'base', |
||
| 210 | title: 'toner', |
||
| 211 | source: new ol.source.Stamen({layer: 'toner'}) |
||
| 212 | }) |
||
| 213 | ); |
||
| 214 | baseLyrGroup.getLayers().push( |
||
| 215 | new ol.layer.Tile({ |
||
| 216 | visible: false, |
||
| 217 | type: 'base', |
||
| 218 | title: 'terrain', |
||
| 219 | source: new ol.source.Stamen({layer: 'terrain'}) |
||
| 220 | }) |
||
| 221 | ); |
||
| 222 | } |
||
| 223 | |||
| 224 | map.addControl(new ol.control.ScaleLine({bar: true, text: true})); |
||
| 225 | map.addControl(new ol.control.MousePosition({ |
||
| 226 | coordinateFormat: ol.coordinate.createStringXY(4), projection: 'EPSG:4326', |
||
| 227 | })); |
||
| 228 | map.addControl(new ol.control.FullScreen()); |
||
| 229 | map.addControl(new ol.control.OverviewMap()); |
||
| 230 | map.addControl(new ol.control.LayerSwitcher()); |
||
| 231 | extent = ol.extent.extend(extent, map.getView().calculateExtent()); |
||
| 232 | |||
| 233 | const iconScale = 1.0; |
||
| 234 | const vectorSource = new ol.source.Vector(); |
||
| 235 | View Code Duplication | poi.forEach((p) => { |
|
| 236 | const f = new ol.Feature({ |
||
| 237 | geometry: new ol.geom.Point(ol.proj.fromLonLat([p.lon, p.lat])), |
||
| 238 | description: p.txt, |
||
| 239 | img: p.img, |
||
| 240 | rowId: p.rowId, |
||
| 241 | lat: p.lat, |
||
| 242 | lon: p.lon, |
||
| 243 | angle: p.angle, |
||
| 244 | alt: p.img.substring(0, p.img.lastIndexOf(".")) |
||
| 245 | }); |
||
| 246 | f.setId(p.rowId); |
||
| 247 | f.setStyle(new ol.style.Style({ |
||
| 248 | text: new ol.style.Text({ |
||
| 249 | text: "" + p.rowId, |
||
| 250 | textAlign: 'left', |
||
| 251 | textBaseline: 'bottom', |
||
| 252 | offsetX: 8, |
||
| 253 | offsetY: -8, |
||
| 254 | scale: iconScale, |
||
| 255 | fill: new ol.style.Fill({color: 'rgb(0,0,0)'}), |
||
| 256 | font: '12px monospace bold', |
||
| 257 | backgroundFill: new ol.style.Fill({color: 'rgba(255,255,255,.4)'}) |
||
| 258 | }), image: new ol.style.Icon({ |
||
| 259 | src: DOKU_BASE + "lib/plugins/openlayersmap/icons/" + p.img, |
||
| 260 | crossOrigin: '', |
||
| 261 | opacity: p.opacity, |
||
| 262 | scale: iconScale, |
||
| 263 | rotation: p.angle * Math.PI / 180, |
||
| 264 | }), |
||
| 265 | })); |
||
| 266 | vectorSource.addFeature(f); |
||
| 267 | }); |
||
| 268 | |||
| 269 | const vectorLayer = new ol.layer.Vector({title: 'POI', visible: true, source: vectorSource}); |
||
| 270 | overlayGroup.getLayers().push(vectorLayer); |
||
| 271 | if (mapOpts.autozoom) { |
||
| 272 | extent = ol.extent.extend(extent, vectorSource.getExtent()); |
||
| 273 | map.getView().fit(extent); |
||
| 274 | } |
||
| 275 | |||
| 276 | if (mapOpts.kmlfile.length > 0) { |
||
| 277 | try { |
||
| 278 | const kmlSource = new ol.source.Vector({ |
||
| 279 | url: DOKU_BASE + "lib/exe/fetch.php?media=" + mapOpts.kmlfile, |
||
| 280 | format: new ol.format.KML(), |
||
| 281 | }); |
||
| 282 | overlayGroup.getLayers().push(new ol.layer.Vector({title: 'KML file', visible: true, source: kmlSource})); |
||
| 283 | |||
| 284 | if (mapOpts.autozoom) { |
||
| 285 | kmlSource.once('change', function () { |
||
| 286 | extent = ol.extent.extend(extent, kmlSource.getExtent()); |
||
| 287 | map.getView().fit(extent); |
||
| 288 | }); |
||
| 289 | } |
||
| 290 | } catch (e) { |
||
| 291 | console.error(e); |
||
| 292 | } |
||
| 293 | } |
||
| 294 | |||
| 295 | if (mapOpts.geojsonfile.length > 0) { |
||
| 296 | try { |
||
| 297 | const geoJsonSource = new ol.source.Vector({ |
||
| 298 | url: DOKU_BASE + "lib/exe/fetch.php?media=" + mapOpts.geojsonfile, |
||
| 299 | format: new ol.format.GeoJSON(), |
||
| 300 | }); |
||
| 301 | overlayGroup.getLayers().push(new ol.layer.Vector({ |
||
| 302 | title: 'GeoJSON file', visible: true, source: geoJsonSource, |
||
| 303 | // TODO |
||
| 304 | // style: { |
||
| 305 | // strokeColor: "#FF00FF", |
||
| 306 | // strokeWidth: 3, |
||
| 307 | // strokeOpacity: 0.7, |
||
| 308 | // pointRadius: 4, |
||
| 309 | // fillColor: "#FF99FF", |
||
| 310 | // fillOpacity: 0.7 |
||
| 311 | // } |
||
| 312 | })); |
||
| 313 | |||
| 314 | if (mapOpts.autozoom) { |
||
| 315 | geoJsonSource.once('change', function () { |
||
| 316 | extent = ol.extent.extend(extent, geoJsonSource.getExtent()); |
||
| 317 | map.getView().fit(extent); |
||
| 318 | }); |
||
| 319 | } |
||
| 320 | } catch (e) { |
||
| 321 | console.error(e); |
||
| 322 | } |
||
| 323 | } |
||
| 324 | |||
| 325 | if (mapOpts.gpxfile.length > 0) { |
||
| 326 | try { |
||
| 327 | const gpxSource = new ol.source.Vector({ |
||
| 328 | url: DOKU_BASE + "lib/exe/fetch.php?media=" + mapOpts.gpxfile, |
||
| 329 | format: new ol.format.GPX(), |
||
| 330 | }); |
||
| 331 | overlayGroup.getLayers().push(new ol.layer.Vector({ |
||
| 332 | title: 'GPS track', visible: true, source: gpxSource, |
||
| 333 | // TODO |
||
| 334 | // style: { |
||
| 335 | // strokeColor: "#0000FF", |
||
| 336 | // strokeWidth: 3, |
||
| 337 | // strokeOpacity: 0.7, |
||
| 338 | // pointRadius: 4, |
||
| 339 | // fillColor: "#0099FF", |
||
| 340 | // fillOpacity: 0.7 |
||
| 341 | // } |
||
| 342 | })); |
||
| 343 | |||
| 344 | if (mapOpts.autozoom) { |
||
| 345 | gpxSource.once('change', function () { |
||
| 346 | extent = ol.extent.extend(extent, gpxSource.getExtent()); |
||
| 347 | map.getView().fit(extent); |
||
| 348 | }); |
||
| 349 | } |
||
| 350 | } catch (e) { |
||
| 351 | console.error(e); |
||
| 352 | } |
||
| 353 | } |
||
| 354 | |||
| 355 | const container = document.getElementById('popup'); |
||
| 356 | const content = document.getElementById('popup-content'); |
||
| 357 | const closer = document.getElementById('popup-closer'); |
||
| 358 | |||
| 359 | const overlay = new ol.Overlay({ |
||
| 360 | element: container, autoPan: true, autoPanAnimation: { |
||
| 361 | duration: 250, |
||
| 362 | }, //stopEvent: false, |
||
| 363 | }); |
||
| 364 | map.addOverlay(overlay); |
||
| 365 | |||
| 366 | /** |
||
| 367 | * Add a click handler to hide the popup. |
||
| 368 | * @return {boolean} Don't follow the href. |
||
| 369 | */ |
||
| 370 | closer.onclick = function () { |
||
| 371 | overlay.setPosition(undefined); |
||
| 372 | closer.blur(); |
||
| 373 | return false; |
||
| 374 | }; |
||
| 375 | |||
| 376 | // display popup on click |
||
| 377 | View Code Duplication | map.on('singleclick', function (evt) { |
|
| 378 | const selFeature = map.forEachFeatureAtPixel(evt.pixel, function (feature) { |
||
| 379 | return feature; |
||
| 380 | }); |
||
| 381 | if (selFeature) { |
||
| 382 | overlay.setPosition(evt.coordinate); |
||
| 383 | |||
| 384 | let pContent = '<div class="spacer"> </div>'; |
||
| 385 | let locDesc = ''; |
||
| 386 | |||
| 387 | if (selFeature.get('rowId') !== undefined) { |
||
| 388 | pContent += '<span class="rowId">' + selFeature.get('rowId') + ': </span>'; |
||
| 389 | } |
||
| 390 | if (selFeature.get('name') !== undefined) { |
||
| 391 | pContent += '<span class="txt">' + selFeature.get('name') + '</span>'; |
||
| 392 | locDesc = selFeature.get('name'); |
||
| 393 | // TODO strip <p> tag from locDesc |
||
| 394 | // locDesc = selFeature.get('name').split(/\s+/).slice(0,2).join('+'); |
||
| 395 | } |
||
| 396 | if (selFeature.get('ele') !== undefined) { |
||
| 397 | pContent += '<div class="ele">elevation: ' + selFeature.get('ele') + '</div>'; |
||
| 398 | } |
||
| 399 | if (selFeature.get('type') !== undefined) { |
||
| 400 | pContent += '<div>' + selFeature.get('type') + '</div>'; |
||
| 401 | } |
||
| 402 | if (selFeature.get('time') !== undefined) { |
||
| 403 | pContent += '<div class="time">time: ' + selFeature.get('time') + '</div>'; |
||
| 404 | } |
||
| 405 | if (selFeature.get('description') !== undefined) { |
||
| 406 | pContent += '<div class="desc">' + selFeature.get('description') + '</div>'; |
||
| 407 | } |
||
| 408 | if (selFeature.get('img') !== undefined) { |
||
| 409 | pContent += '<div class="coord" title="lat;lon">' + |
||
| 410 | '<img src="' + DOKU_BASE + 'lib/plugins/openlayersmap/icons/' + selFeature.get('img') + |
||
| 411 | '" width="16" height="16" ' + 'style="transform:rotate(' + selFeature.get('angle') + 'deg)" /> ' + |
||
| 412 | '<a href="geo:' + selFeature.get('lat') + ',' + selFeature.get('lon') + '?q=' + selFeature.get('lat') + |
||
| 413 | ',' + selFeature.get('lon') + '(' + selFeature.get('alt') + ')" title="Open in navigation app">' + |
||
| 414 | ol.coordinate.format([selFeature.get('lon'), selFeature.get('lat')], '{x}º; {y}º', 4) + '</a></div>'; |
||
| 415 | } |
||
| 416 | content.innerHTML = pContent; |
||
| 417 | } else { |
||
| 418 | // do nothing... |
||
| 419 | } |
||
| 420 | }); |
||
| 421 | |||
| 422 | // change mouse cursor when over marker |
||
| 423 | map.on('pointermove', function (e) { |
||
| 424 | const pixel = map.getEventPixel(e.originalEvent); |
||
| 425 | const hit = map.hasFeatureAtPixel(pixel); |
||
| 426 | map.getTarget().style.cursor = hit ? 'pointer' : ''; |
||
| 427 | }); |
||
| 428 | |||
| 429 | return map; |
||
| 430 | } |
||
| 431 | |||
| 559 |